package com.xiaoge.usemvpplugin.base_mvp;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by guoyzh on 2018/8/13 0013.
 */
public abstract class BaseMvpFragment<P extends BasePresenter, M extends BaseModel> extends Fragment {

    private P mPresenter;
    private M mModel;
    private View view;
    private ViewGroup viewGroup;
    private Context mContext;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(getLayoutResId(), container, false);
        this.viewGroup = container;
        mContext = getActivity();

        //内部获取第一个类型参数的真实类型  ，反射new出对象
        mPresenter = CreateUtil.getT(this, 0);
        //内部获取第二个类型参数的真实类型  ，反射new出对象
        mModel = CreateUtil.getT(this, 1);
        //使得P层绑定M层和V层，持有M和V的引用
        mPresenter.attachModelView(mModel, this);

        initView(view, getArguments());
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.onDettach();
    }

    //子类Activity实现
    public abstract int getLayoutResId();

    public abstract void initView(View view, Bundle arguments);
}

